import { resolve } from 'path';
import { defineConfig } from 'vite';

import vue from '@vitejs/plugin-vue';
import { VitePWA } from 'vite-plugin-pwa';
import compression from 'vite-plugin-compression';
import { createStyleImportPlugin, VantResolve } from 'vite-plugin-style-import';

const outDir = '../web/wwwroot/mobile';
const targetUrl = 'http://127.0.0.1:8881/';

// https://vitejs.dev/config/
export default defineConfig({
  css: {
    preprocessorOptions: {
      less: {
        javascriptEnabled: true
      }
    }
  },
  plugins: [
    vue(),
    createStyleImportPlugin({
      resolves: [VantResolve],
      libs: [
        {
          libraryName: 'vant',
          esModule: true,
          resolveStyle: name => `../es/${name}/style`
        }
      ]
    }),
    compression(),
    VitePWA({
      base: '/',
      registerType: 'autoUpdate',
      filename: 'sw.mobile.js',
      injectRegister: 'inline',
      manifestFilename: 'manifest.mobile',
      manifest: {
        id: '/',
        name: '个人帮助系统 - 移动端',
        short_name: 'PHS - Mobile',
        icons: [{
          src: '/phs.png',
          sizes: '144x144',
          type: 'image/png'
        }],
        start_url: '/'
      },
      workbox: {
        globIgnores: ['index.html']
      }
    })
  ],
  resolve: {
    alias: {
      '@': resolve(__dirname, './src')
    }
  },
  server: {
    open: true,
    host: '0.0.0.0',
    port: 7200,
    cors: true,
    proxy: {
      '^/api': { ws: true, secure: false, changeOrigin: true, target: targetUrl },
      '^/.+/api': { ws: true, secure: false, changeOrigin: true, target: targetUrl }
    }
  },
  build: {
    target: 'es6',
    outDir: outDir,
    minify: 'esbuild',
    emptyOutDir: true,
    // rollupOptions: {
    //   input: {
    //     main: resolve(__dirname, 'mobile.html')
    //   }
    // },
    // terserOptions: {
    //   nameCache: false,
    //   compress: {
    //     keep_infinity: true,
    //     drop_console: true,
    //     drop_debugger: true,
    //   }
    // },
    // Turning off brotliSize display can slightly reduce packaging time
    brotliSize: false,
    chunkSizeWarningLimit: 2000
  }
});
